package com.zhenyi.liuxue.mini;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cjw.model.DBMap;
import com.jfinal.aop.Clear;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.liuxue.common.model.LxCnSchool;
import com.liuxue.common.model.LxCompany;
import com.liuxue.common.model.LxDegreeCertificate;
import com.liuxue.common.model.LxUser;
import com.utils.AESUtils;
import com.utils.AuthUtils;
import com.zhenyi.core.AES;
import com.zhenyi.liuxue.core.SMSUtils;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class UserController extends MiniController {

	private final String AES_KEY = "liuxue!@#$%^&*()";
	private final String APP_ID = PropKit.get("LX_APP_ID", "wx7ca6625ef7867aa7");
	private final String APP_SECRET = PropKit.get("LX_APP_SECRET", "aa59a2e17a1747821a9387f5677ee4e1");

	private final String CACHE_KEY = "-superlink";

	/**
	 * 个人信息
	 */
	@Override
	public void info() {
		LxUser user = getUser();
		if (user != null && !user.getStatus().equals(0)) {
			DBMap map = new DBMap();
			map.put("avatar", user.getAvatar());
			String name = user.getName();
			if (StrKit.isBlank(name)) {
				name = user.getWechatNickname();
			}
			map.put("name", name);
			map.put("id", user.getId());
			// user.set("point", "计算中");
			renderSuccess(user);
		} else {
			renderFailure(401, "please login");
		}
	}

	/**
	 * 修改个人资料
	 */
	@Override
	public void submit() {
		Long id = getParaToLong("id");
		String account = getPara("account");
		String password = getPara("password");
		String name = getPara("name");
		String englishName = getPara("englishName");
		String lastName = getPara("lastName");
		String firstName = getPara("firstName");
		Integer sex = getParaToInt("sex");
		String location = getPara("location");
		Long companyId = getParaToLong("companyId");
		String country = getPara("country");
		String department = getPara("department");
		String startDate = getPara("startDate");
		// String mobile = getPara("mobile");
		String email = getPara("email");
		String degree = getPara("degree");
		String major = getPara("major");
		String hobby = getPara("hobby");
		// String avatar = getPara("avatar");
		String card = getPara("card");
		Long postgradSchool = getParaToLong("postgradSchool");
		String postgradMajor = getPara("postgradMajor");
		Long undergradSchool = getParaToLong("undergradSchool");
		String undergradMajor = getPara("undergradMajor");
		String province = getPara("province");
		String city = getPara("city");
		String region = getPara("region");
		String formId = getPara("formId");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		if (StrKit.isBlank(password)) {
			renderFailure(500, "请输入密码");
			return;
		}
		password = AESUtils.decrypt(AES_KEY, password);
		// LxUser info = LxUser.dao.findById(id);
		LxUser info = getUser();
		if (info == null) {
			renderFailure(404, "not found: invalid id");
			return;
		}
		info.setAccount(account);
		info.setPassword(password);
		info.setName(name);
		info.setEnglishName(englishName);
		info.setLastName(lastName);
		info.setFirstName(firstName);
		info.setSex(sex);
		info.setLocation(location);
		info.setCompanyId(companyId);
		info.setCountry(country);
		info.setDepartment(department);
		info.setStartDate(startDate);
		info.setMobile(info.getAccount());
		info.setEmail(email);
		info.setDegree(degree);
		info.setMajor(major);
		info.setHobby(hobby);
		// info.setAvatar(avatar);
		info.setCard(card);
		info.setPostgradSchool(postgradSchool);
		info.setPostgradMajor(postgradMajor);
		info.setUndergradSchool(undergradSchool);
		info.setUndergradMajor(undergradMajor);
		info.setProvince(province);
		info.setCity(city);
		info.setRegion(region);
		info.setFormId(formId);
		if (info.update()) {
			AuthUtils.login(String.valueOf(info.getId()), this);
			renderSuccess(info);
		} else {
			renderFailure();
		}
	}

	private final String code2SessionUrl = "https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code";

	@Clear
	public void auth() {
		String code = getPara("code");
		if (StrKit.isBlank(code)) {
			renderFailure();
			return;
		}
		String country = getPara("country");
		Integer gender = getParaToInt("gender", 0);
		String province = getPara("province");
		String avatarUrl = getPara("avatarUrl");
		String nickName = getPara("nickName");
		String url = StrFormatter.format(code2SessionUrl, APP_ID, APP_SECRET, code);
		// System.out.println(url);
		String response = HttpKit.get(url);
		// System.out.println(response);
		JSONObject jsonObject = JSONUtil.parseObj(response);
		String openId = jsonObject.getStr("openid");
		String session_key = jsonObject.getStr("session_key");
		String unionid = jsonObject.getStr("unionid");
		Integer errcode = jsonObject.getInt("errcode");
		if (errcode != null) {
			String errmsg = jsonObject.getStr("errmsg");
			LogKit.warn("jscode2session error " + errmsg);
			renderFailure();
			return;
		}
		Kv kv = Kv.by("openId", openId);
		SqlPara sp = db.getSqlPara("LXFindUser", kv);
		LxUser wechat = LxUser.dao.findFirst(sp);
		// LxUser wechat = LxUser.dao.findByUnionId(unionid);
		if (wechat == null || wechat.getStatus().equals(0)) {
			renderFailure("未找到绑定用户");
			DBMap map = new DBMap();
			map.put("unionId", unionid);
			map.put("openId", openId);
			renderResult(map, "ok", 206);
			return;
		}

		// if (wechat == null) {
		// wechat = new XhcWechat();
		// wechat.save();
		// wechat.setOpenId(openId);
		// }
		// wechat.setSessionKey(session_key);
		// if (StrKit.notBlank(country)) {
		// wechat.setCountry(country);
		// }
		// if (StrKit.notBlank(province)) {
		// wechat.setProvince(province);
		// }
		if (StrKit.notBlank(avatarUrl)) {
			wechat.setAvatar(avatarUrl);
		}
		if (StrKit.notBlank(nickName)) {
			wechat.setWechatNickname(nickName);
		}
		// if (gender != null && gender == 1) {
		// wechat.setSex(0);
		// } else if (gender != null && gender == 2) {
		// wechat.setSex(1);
		// }
		// wechat.setNickName(nickName);
		// wechat.setAppid(APP_ID);
		// wechat.setLastLogin(new Date());
		wechat.update();
		DBMap map = new DBMap();
		String token = AuthUtils.login(String.valueOf(wechat.getId()), this);
		map.put("token", token);
		map.put("user", wechat);
		map.put("openId", wechat.getOpenId());
		map.put("unionId", wechat.getUnionId());

		Cache cache = Redis.use();
		cache.setex(openId + CACHE_KEY, 3600, session_key);

		renderSuccess(map);
	}

	/**
	 * 我的积分
	 */
	public void point() {
		LxUser user = getUser();
		DBMap map = new DBMap();
		map.put("status", 200);
		map.put("msg", "ok");
		map.put("info", user.getPoint());
		renderJson(map);
	}

	/**
	 * 我的证书
	 */
	public void cert() {
		LxUser user = getUser();
		Kv kv = Kv.create();
		kv.set("userId", user.getId());
		// String appName =
		// "?watermark/3/text/{}/font/dGltZXMgbmV3IHJvbWFu/fontsize/500/fill/IyM2NDY0NjQ=/dissolve/85/gravity/Center/dx/0/dy/-20/text/U3VwZXJsaW5rcGx1cw==/font/dGltZXMgbmV3IHJvbWFu/fontsize/500/fill/IyM2NDY0NjQ=/dissolve/85/gravity/Center/dx/0/dy/55";
		// appName = StrFormatter.format(appName, user.getEnglishName());
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindUserCerts", kv);
		Page<Record> pages = Db.use("liuxue").paginate(getPage(), PAGE_SIZE, sqlPara);
		renderSuccess(pages);
	}

	public void company() {
		String keyword = getPara("keyword");
		Integer noPage = getParaToInt("noPage");

		Kv kv = Kv.create();
		if (StrKit.notBlank(keyword)) {
			kv.set("keyword", keyword);
		}
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindCompanies", kv);

		if (noPage != null && noPage.equals(1)) {
			List<LxCompany> list = LxCompany.dao.find(sqlPara);
			renderSuccess(list);
			return;
		}
		Page<LxCompany> pages = LxCompany.dao.paginate(getPage(), PAGE_SIZE, sqlPara);
		renderSuccess(pages);
	}

	public void companyInfo() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		LxCompany info = LxCompany.dao.findById(id);
		if (info == null) {
			renderFailure(404, "not found");
			return;
		}
		renderSuccess(info);
	}

	/**
	 * h5 选择公司页面
	 */
	public void selectCompany() {
		render("/lx/activity/selectCompany.html");
	}

	/**
	 * h5 选择学校页面
	 * 
	 * @param type
	 *            0本科院校 1硕士院校
	 */
	public void selectSchool() {
		Integer type = getParaToInt("type");
		if (type == null) {
			renderFailure();
			return;
		}
		setAttr("type", type);
		render("/lx/activity/selectSchool.html");
	}

	private Ret checkUser(String account, String password) {
		String url = "http://www.superlink.mobi/index.php/Api/User/login";
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("appkey", "1c038440c9a079ba206daa0405315f86");
		paramMap.put("mobile", account);
		paramMap.put("password", password);
		String response = HttpUtil.post(url, paramMap);
		JSONObject jsonObject = JSONUtil.parseObj(response);
		int error = jsonObject.getInt("error");
		if (error == 0) {
			JSONObject data = jsonObject.getJSONObject("data");
			String uid = data.getStr("uid");
			return Ret.ok("uid", uid);
		} else {
			return Ret.fail();
		}
	}

	/**
	 * 绑定用户
	 */
	@Clear
	public void bind() {
		String account = getPara("account");
		String openId = getPara("openId");
		String unionId = getPara("unionId");
		// String password = getPara("password");
		String code = getPara("code");
		if (!DEBUG) {
			if (!SMSUtils.verify(account, code)) {
				renderFailure(400, "验证码错误或已失效");
				return;
			}
		}

		Kv kv = Kv.by("account", account);
		// kv.set("password", password);
		// if (!StrKit.notBlank(password, account)) {
		if (StrKit.isBlank(account)) {
			renderFailure();
			return;
		}

		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindBindUser", kv);
		LxUser user = LxUser.dao.findFirst(sqlPara);
		// if (user == null) {
		// Ret ret = checkUser(account, password);
		// if (ret.isFail()) {
		// LogKit.info("pwd error");
		// renderFailure("账号/密码错误");
		// return;
		// }
		// String uid = ret.getStr("uid");
		// user = LxUser.dao.findById(uid);
		// user.setPassword(password);
		// LogKit.info("find user");
		// } else {
		// LogKit.info("already bind");
		// }
		if (user == null || user.getStatus().equals(0)) {
			DBMap map = new DBMap();
			map.put("isRegistered", 0);
			map.put("account", account);
			renderSuccess(map);
			return;
		}

		DBMap map = new DBMap();
		// if (user == null) {
		// if (checkUser(account, password).isFail()) {
		// map.put("isRegistered", 0);
		// // renderSuccess(map);
		// renderFailure("账号/密码错误");
		// return;
		// } else {
		// user.setPassword(password);
		// }
		// }

		if (StrKit.notBlank(openId)) {
			user.setOpenId(openId);
		}
		if (StrKit.notBlank(unionId)) {
			user.setUnionId(unionId);
		}
		user.update();
		String token = AuthUtils.login(String.valueOf(user.getId()), this);
		map.put("isRegistered", 1);
		map.put("openId", openId);
		map.put("user", user);
		map.put("token", token);
		renderSuccess(map);
	}

	public void wechatLogin() {
		String code = getPara("code");
		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("appid", APP_ID);
		paramMap.put("secret", APP_SECRET);
		paramMap.put("js_code", code);
		paramMap.put("grant_type", "authorization_code");
		String body = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", paramMap);
		JSONObject response = JSONUtil.parseObj(body);
		String sessionKey = response.getStr("session_key");
		String openId = response.getStr("openid");

		Cache cache = Redis.use();
		cache.setex(openId + CACHE_KEY, 3600, sessionKey);

		Kv kv = Kv.by("openId", openId);
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindUser", kv);
		LxUser user = LxUser.dao.findFirst(sqlPara);
		DBMap map = new DBMap();
		if (user == null || user.getStatus() == 0) {
			map.put("isRegistered", 0);
		} else {
			map.put("isRegistered", 1);
			map.put("user", user);
		}
		renderSuccess(map);
	}

	public void init() {
		String code = getPara("code");
		String nickName = getPara("nickName");
		String avatarUrl = getPara("avatarUrl");
		Integer gender = getParaToInt("gender");
		String province = getPara("province");
		String country = getPara("country");
		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("appid", APP_ID);
		paramMap.put("secret", APP_SECRET);
		paramMap.put("js_code", code);
		paramMap.put("grant_type", "authorization_code");
		String body = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", paramMap);
		JSONObject response = JSONUtil.parseObj(body);
		String sessionKey = response.getStr("session_key");
		String openId = response.getStr("openid");

		Kv kv = Kv.by("openId", openId);
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindUser", kv);
		LxUser user = LxUser.dao.findFirst(sqlPara);
		if (user == null) {
			user = new LxUser();
			user.setOpenId(openId);
			user.setWechatNickname(nickName);
			user.setWechatPortrait(avatarUrl);
			if (gender != null && gender == 1) {
				user.setSex(0);
			} else if (gender != null && gender == 2) {
				user.setSex(1);
			}
			user.setProvince(province);
			user.setCountry(country);
			user.setStatus(0);
			user.save();
		}
		user.setSessionKey(sessionKey);
		user.update();

		renderSuccess(openId);
	}

	public void checkPhoneNumber() {
		String encryptedData = getPara("encryptedData");
		String iv = getPara("iv");
		String openId = getPara("openId");
		String nickName = getPara("nickName");
		String avatarUrl = getPara("avatarUrl");
		Integer gender = getParaToInt("gender");
		String province = getPara("province");
		String city = getPara("city");
		String country = getPara("country");

		Cache cache = Redis.use();
		String sessionKey = cache.get(openId + CACHE_KEY);

		String decrypted = AES.de(encryptedData, sessionKey, iv);
		if (decrypted == null) {
			System.out.println("sessionKey: " + sessionKey);
			System.out.println("openId: " + openId);
			renderFailure();
		}
		JSONObject obj = JSONUtil.parseObj(decrypted);
		String account = obj.getStr("phoneNumber");

		Kv kv = Kv.by("account", account);
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindUser", kv);
		LxUser user = LxUser.dao.findFirst(sqlPara);
		DBMap map = new DBMap();
		if (user == null || user.getStatus() == 0) {
			user = new LxUser();
			user.setOpenId(openId);
			user.setSessionKey(sessionKey);
			user.setAccount(account);
			user.setMobile(account);
			user.setWechatNickname(nickName);
			user.setWechatPortrait(avatarUrl);
			if (gender != null && gender == 1) {
				user.setSex(0);
			} else if (gender != null && gender == 2) {
				user.setSex(1);
			}
			user.setProvince(province);
			user.setCity(city);
			user.setStatus(0);
			user.save();

			map.put("isRegistered", 0);
			map.put("id", user.getId());
			map.put("account", user.getAccount());
		} else {
			map.put("isRegistered", 1);
			map.put("user", user);
		}
		renderSuccess(map);
	}

	/**
	 * h5 登录页面
	 */
	public void signIn() {
		// render("/lx/activity/login.html");
		String ref = getCookie("ref", "");
		System.out.println(ref);
		setAttr("ref", ref);
		render("/lx/activity/validateMobile.html");
	}

	/**
	 * h5 注册页面
	 */
	public void signUp() {
		// Long uid = getParaToLong("uid");
		// LxUser user = LxUser.dao.findById(uid);
		// setAttr("uid", uid);
		// setAttr("account", user.getAccount());
		String account = getPara("account");
		setAttr("account", account);
		render("/lx/activity/signUp.html");
	}

	/**
	 * h5 验证手机页面
	 */
	public void valiMobile() {
		render("/lx/activity/validateMobile.html");
	}

	public void login() {
		String account = getPara("account");
		if (StrKit.isBlank(account)) {
			renderFailure(500, "请输入账号");
			return;
		}
		String password = getPara("password");
		try {
			// String key = getSessionAttr("key");
			// System.out.println(key);
			password = AESUtils.decrypt(AES_KEY, password);
			if (password == null) {
				renderFailure(500, "解密失败,请刷新重试");
				return;
			}
		} catch (Exception e) {
			LogKit.warn(account + "登录失败", e);
			renderFailure();
			return;
		}
		DBMap maps = new DBMap();
		maps.put("account", account);
		maps.put("password", password);
		LxUser user = LxUser.dao.searchFirst(maps);
		if (user == null) {
			renderFailure(500, "账号/密码错误");
			return;
		}
		String token = AuthUtils.login(String.valueOf(user.getId()), this);
		renderSuccess(user);
	}

	public void logout() {
		AuthUtils.logout(this);
		renderSuccess();
	}

	public void sendCode() {
		String mobile = getPara("mobile");
		if (StrKit.isBlank(mobile)) {
			renderFailure(500, "请输入手机号");
			return;
		}
		if (SMSUtils.app2eSend(mobile)) {
			renderSuccess();
		} else {
			renderFailure(500, "短信发送失败，请稍后再试");
		}
	}

	public void validateMobile() {
		String mobile = getPara("mobile");
		String code = getPara("code");
		if (!SMSUtils.verify(mobile, code)) {
			renderFailure(400, "验证码错误或已失效");
			return;
		}

		Kv kv = Kv.by("account", mobile);
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindUser", kv);
		LxUser info = LxUser.dao.findFirst(sqlPara);
		if (info != null && info.getStatus() != 0) {
			// renderFailure(500, "该手机号已注册");
			AuthUtils.login(String.valueOf(info.getId()), this);
			renderSuccess(info);
			return;
		}
		boolean isNew = info == null;
		if (isNew) {
			info = new LxUser();
		}
		info.setAccount(mobile);
		info.setFlag(0);
		info.setStatus(0);
		if (isNew) {
			info.save();
		} else {
			info.update();
		}

		renderSuccess(info);
	}

	public void register() {
		// Long id = getParaToLong("id");
		String account = getPara("account");
		String password = getPara("password");
		String name = getPara("name");
		String englishName = getPara("englishName");
		String lastName = getPara("lastName");
		String firstName = getPara("firstName");
		Integer sex = getParaToInt("sex");
		String location = getPara("location");
		Long companyId = getParaToLong("companyId");
		String country = getPara("country");
		String department = getPara("department");
		String startDate = getPara("startDate");
		// String mobile = getPara("mobile");
		String email = getPara("email");
		String degree = getPara("degree");
		String major = getPara("major");
		String hobby = getPara("hobby");
		String avatar = getPara("avatar");
		String card = getPara("card");
		Long postgradSchool = getParaToLong("postgradSchool");
		String postgradMajor = getPara("postgradMajor");
		Long undergradSchool = getParaToLong("undergradSchool");
		String undergradMajor = getPara("undergradMajor");
		String province = getPara("province");
		String city = getPara("city");
		String region = getPara("region");
		String openId = getPara("openId");
		String wechatPortrait = getPara("wechatPortrait");
		String wechatNickname = getPara("wechatNickname");
		String formId = getPara("formId");
		// if (id == null) {
		// renderFailure(500, "id is null");
		// return;
		// }
		// if (StrKit.isBlank(password)) {
		// renderFailure(500, "请输入密码");
		// return;
		// }
		password = AESUtils.decrypt(AES_KEY, password);

		// LxUser info = LxUser.dao.searchFirst("account", account);
		if (StrKit.isBlank(account)) {
			renderFailure(500, "手机号不能为空");
			return;
		}
		Kv kv = Kv.by("account", account);
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindBindUser", kv);
		LxUser info = LxUser.dao.findFirst(sqlPara);
		if (info != null && !info.getStatus().equals(0)) {
			renderFailure(500, "账号已存在");
			return;
		} else {
			info = LxUser.dao.findByOpenId(openId);
		}
		// LxUser info = LxUser.dao.findById(id);
		if (info == null) {
			// renderFailure(404, "not found: invalid id");
			// return;
			info = new LxUser();
			info.save();
		}
		info.setAccount(account);
		info.setPassword(password);
		info.setName(name);
		info.setEnglishName(englishName);
		info.setLastName(lastName);
		info.setFirstName(firstName);
		info.setSex(sex);
		info.setLocation(location);
		info.setCompanyId(companyId);
		info.setCountry(country);
		info.setDepartment(department);
		info.setStartDate(startDate);
		info.setMobile(info.getAccount());
		info.setEmail(email);
		info.setDegree(degree);
		info.setMajor(major);
		info.setHobby(hobby);
		info.setAvatar(avatar);
		info.setCard(card);
		info.setPostgradSchool(postgradSchool);
		info.setPostgradMajor(postgradMajor);
		info.setUndergradSchool(undergradSchool);
		info.setUndergradMajor(undergradMajor);
		info.setProvince(province);
		info.setCity(city);
		info.setRegion(region);
		info.setFlag(null);
		info.setStatus(1);
		info.setOpenId(openId);
		if (info.getPoint() == null || info.getPoint().equals(0)) {
			info.setPoint(100);
		}
		info.setWechatPortrait(wechatPortrait);
		info.setWechatNickname(wechatNickname);
		info.setFormId(formId);
		if (info.update()) {
			AuthUtils.login(String.valueOf(info.getId()), this);
			renderSuccess(info);
		} else {
			renderFailure();
		}
	}

	/**
	 * 修改密码
	 */
	public void changePassword() {
	}

	public void schools() {
		String kw = getPara("kw");
		String country = getPara("country");

		String from = "FROM lx_degree_certificate";
		String where = " WHERE lx_degree_certificate.country = '" + country + "'";
		String totalRowSql = "SELECT count(*) " + from + where;
		String findSql = null;
		String kwSql = " AND (lx_degree_certificate.certificateName LIKE '%" + kw + "%'"
				+ "OR lx_degree_certificate.certificateNameEn LIKE '%" + kw + "%')";
		// String orderBy = " ORDER BY
		// convert(lx_degree_certificate.certificateName using gbk) ASC";
		String orderBy = " ORDER BY lx_degree_certificate.certificateNameEn ASC";
		if (StrKit.isBlank(kw)) {
			findSql = "SELECT lx_degree_certificate.* " + from + where + orderBy;
		} else {
			findSql = "SELECT lx_degree_certificate.* " + from + where + kwSql + orderBy;
		}
		Page<LxCnSchool> page = LxCnSchool.dao.paginateByFullSql(getPage(), PAGE_SIZE, totalRowSql, findSql);
		renderResult(page);
	}

	public void schoolInfo() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		LxDegreeCertificate info = LxDegreeCertificate.dao.findById(id);
		if (info == null) {
			renderFailure(404, "not found");
			return;
		}
		renderSuccess(info);
	}

	public void schoolCountries() {
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindDegreeCertCountry");
		List<Record> list = Db.use("liuxue").find(sqlPara);
		String[] result = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			result[i] = list.get(i).getStr("country");
		}
		renderResult(result);
	}
}
